//
//  SEGInviteView.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/2/23.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGInviteView.h"
@interface SEGInviteView ()
{
    CGFloat viewHeight;
}
@end

@implementation SEGInviteView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.bounces = NO;
        [self initSubviews];
    }
    return self;
}

- (void)initSubviews
{
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor colorWithRed:254/255.0 green:231/255.0 blue:187/255.0 alpha:1];
    [self addSubview:contentView];
    self.contentSize = CGSizeMake(LYScreenWidth(), LYScreenHeight());
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self);
        make.height.mas_equalTo(LYScreenHeight());
        make.width.mas_equalTo(LYScreenWidth());
    }];
    self.contentView = contentView;
    viewHeight = 0;
    
    //头部背景图片 服务器返回
    UIImageView *topBgView = [[UIImageView alloc] init];
    [contentView addSubview:topBgView];
    //适配小屏
    CGFloat topHeight = 334;
    if (LYScreenWidth() < 375.0) {
        topHeight = topHeight * (LYScreenWidth()/375.0);
    }
    [topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(topHeight);
    }];
    self.topAdImage = topBgView;
    viewHeight += topHeight;
    
    //分享二维码按钮
    UIButton *QRCodeButton = [[UIButton alloc] init];
    [QRCodeButton setTitle:@"扫码\n邀请" forState:UIControlStateNormal];
    [QRCodeButton setTitleColor:[UIColor colorWithRed:206/255.0 green:65/255.0 blue:46/255.0 alpha:1] forState:UIControlStateNormal];
    QRCodeButton.titleLabel.font = [UIFont systemFontOfSize:11];
    QRCodeButton.titleLabel.numberOfLines = 2;
    QRCodeButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    [QRCodeButton setBackgroundImage:[UIImage imageWithContentsOfFile:SEGPicName(@"img_invite_tipbg_y")] forState:UIControlStateNormal];
    [contentView addSubview:QRCodeButton];
    [QRCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentView.mas_top).with.offset(15);
        make.right.equalTo(contentView.mas_right);
        make.width.mas_equalTo(113/2.0);
        make.height.mas_equalTo(33);
    }];
    self.qRCodeInviteButton = QRCodeButton;
    [self.qRCodeInviteButton addTarget:self action:@selector(qRCodeInviteButtonPress) forControlEvents:UIControlEventTouchUpInside];
    
    //中部 邀请记录
    UIImageView *recordImageView = [[UIImageView alloc] init];
    recordImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_invite_record")];
    [contentView addSubview:recordImageView];
    [recordImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topBgView.mas_bottom).with.offset(30);
        make.left.equalTo(contentView.mas_left).with.offset(30);
        make.width.mas_equalTo(85);
        make.height.mas_equalTo(70);
    }];
    viewHeight += (30 + 70);
    
    UIImageView *dashedImageView = [[UIImageView alloc] init];
    dashedImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_invite_dashed")];
    [contentView addSubview:dashedImageView];
    [dashedImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(recordImageView.mas_centerY);
        make.left.equalTo(recordImageView.mas_right).with.offset(30);
    }];
    
    UILabel *countLabel = [[UILabel alloc] init];
    countLabel.font = [UIFont systemFontOfSize:14];
    countLabel.textColor = [UIColor colorWithRed:159/255.0 green:90/255.0 blue:51/255.0 alpha:1];
//    countLabel.text = [NSString stringWithFormat:@"已成功邀请%ld名邻居",(long)[_model.total integerValue]];
    [contentView addSubview:countLabel];
    [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dashedImageView.mas_top).with.offset(1);
        make.left.equalTo(dashedImageView.mas_right).with.offset(30);
    }];
    self.inviteCountLabel = countLabel;
    
    //查看详细记录按钮
    UIButton *recordButton = [[UIButton alloc] init];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"查看详细记录 >>"];
    NSRange strRange = {0,[str length]};
    [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:159/255.0 green:90/255.0 blue:51/255.0 alpha:1] range:strRange];
    [recordButton setAttributedTitle:str forState:UIControlStateNormal];
    recordButton.titleLabel.font = [UIFont systemFontOfSize:13];
    recordButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    [contentView addSubview:recordButton];
    [recordButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(countLabel.mas_bottom).with.offset(10);
        make.left.equalTo(countLabel.mas_left);
        make.height.mas_equalTo(18);
    }];
    self.inviteRecordButton = recordButton;
    [self.inviteRecordButton addTarget:self action:@selector(inviteRecordButtonPress) forControlEvents:UIControlEventTouchUpInside];
    
    //立即邀请邻居按钮
    UIButton *urlInviteButton = [[UIButton alloc] init];
    urlInviteButton.titleLabel.font = [UIFont systemFontOfSize:13];
    urlInviteButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    [urlInviteButton setBackgroundImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_invite_botton_nor")] forState:UIControlStateNormal];
    [urlInviteButton setBackgroundImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_invite_botton_pre")] forState:UIControlStateHighlighted];
    [contentView addSubview:urlInviteButton];
    [urlInviteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(recordImageView.mas_bottom).with.offset(45);
        make.centerX.equalTo(contentView.mas_centerX);
//        make.height.mas_equalTo(77);
    }];
    self.urlInviteButton = urlInviteButton;
    [self.urlInviteButton addTarget:self action:@selector(urlInviteButtonPress) forControlEvents:UIControlEventTouchUpInside];
    viewHeight += (45 + 77);
    
    //邀请规则
    UILabel *ruleTitleLabel = [[UILabel alloc] init];
    ruleTitleLabel.font = [UIFont systemFontOfSize:13];
    ruleTitleLabel.textColor = [UIColor colorWithRed:159/255.0 green:90/255.0 blue:51/255.0 alpha:1];
    ruleTitleLabel.text = @"邀请规则";
    [contentView addSubview:ruleTitleLabel];
    [ruleTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(urlInviteButton.mas_bottom).with.offset(45);
        make.centerX.equalTo(contentView.mas_centerX);
    }];
    viewHeight += (45 + 13);
    
    UIImageView *ruleLineLeft = [[UIImageView alloc] init];
    UIEdgeInsets insets = UIEdgeInsetsMake(0, 10, 0, 0);
    ruleLineLeft.image = [[UIImage imageWithContentsOfFile:SEGPicName(@"btn_invite_dotsline_l")] resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
    [contentView addSubview:ruleLineLeft];
    [ruleLineLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(ruleTitleLabel.mas_centerY);
        make.left.equalTo(contentView.mas_left).with.offset(15);
        make.right.equalTo(ruleTitleLabel.mas_left).with.offset(-15);
    }];
    UIImageView *ruleLineRight = [[UIImageView alloc] init];
    insets = UIEdgeInsetsMake(0, 0, 0, 10);
    ruleLineRight.image = [[UIImage imageWithContentsOfFile:SEGPicName(@"btn_invite_dotsline_r")] resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
    [contentView addSubview:ruleLineRight];
    [ruleLineRight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(ruleTitleLabel.mas_centerY);
        make.left.equalTo(ruleTitleLabel.mas_right).with.offset(15);
        make.right.equalTo(contentView.mas_right).with.offset(-15);
    }];
    
    UILabel *ruleTextLabel = [[UILabel alloc] init];
    ruleTextLabel.font = [UIFont systemFontOfSize:13];
    ruleTextLabel.textColor = [UIColor colorWithRed:159/255.0 green:90/255.0 blue:51/255.0 alpha:1];
    ruleTextLabel.numberOfLines = 0;
    [contentView addSubview:ruleTextLabel];
    [ruleTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ruleTitleLabel.mas_bottom).with.offset(30);
        make.left.equalTo(contentView.mas_left).with.offset(15);
        make.right.equalTo(contentView.mas_right).with.offset(-15);
    }];
    self.rulesLabel = ruleTextLabel;
    
    //底部背景图片
    UIImageView *bottomBgView = [[UIImageView alloc] init];
    bottomBgView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_invite_bottombg")];
    [contentView addSubview:bottomBgView];
    [bottomBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(contentView);
        make.height.mas_equalTo(120);
    }];
    self.bottomBgView = bottomBgView;
    viewHeight += 120;
}

- (void)setModel:(SEGInviteModel *)model
{
    _model = model;
    
    [self.topAdImage setImageWithURL:[NSURL URLWithString:_model.bannerUrl] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_1125_1125")]];
    
    NSMutableAttributedString* str1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"已成功邀请 %ld 名邻居",(long)[_model.total integerValue]]];
    [str1 addAttribute:NSFontAttributeName
                 value:[UIFont systemFontOfSize:18]
                 range:NSMakeRange([str1 length] - _model.total.length - 4, _model.total.length)];
    self.inviteCountLabel.attributedText = str1;
    
    NSMutableString *rules = [NSMutableString string];
    NSUInteger count = MIN(_model.rules.count, 3);
    for (int i = 0; i < count ; i++) {
        [rules appendString:[NSString stringWithFormat:@"%@\n\n",_model.rules[i]]];
    }
    self.rulesLabel.text = rules;
    
    if (_model.rules.count > 3) {
        //查看更多按钮
        UIButton *moreRuleButton = [[UIButton alloc] init];
        NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc] initWithString:@"查看更多 >>"];
        NSRange strRange2 = {0,[str2 length]};
        [str2 addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange2];
        [str2 addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:159/255.0 green:90/255.0 blue:51/255.0 alpha:1] range:strRange2];
        [moreRuleButton setAttributedTitle:str2 forState:UIControlStateNormal];
        moreRuleButton.titleLabel.font = [UIFont systemFontOfSize:13];
        moreRuleButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        [self.contentView addSubview:moreRuleButton];
        [moreRuleButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.bottomBgView.mas_top);
            make.height.mas_equalTo(15);
            make.centerX.mas_equalTo(self.contentView.mas_centerX);
        }];
        self.showMoreRuleButton = moreRuleButton;
        [self.showMoreRuleButton addTarget:self action:@selector(showMoreRuleButtonPress) forControlEvents:UIControlEventTouchUpInside];
        viewHeight += 15;
    }
    
    // 计算规则高度 调整界面
    CGSize sizeToFit = [rules sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(LYScreenWidth()-30, 10000) lineBreakMode:NSLineBreakByCharWrapping];
    viewHeight += sizeToFit.height;
    self.contentSize = CGSizeMake(LYScreenWidth(), viewHeight);
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(viewHeight);
    }];
}

//点击扫码邀请
- (void)qRCodeInviteButtonPress
{
    if (self.viewDelegate && [self.viewDelegate respondsToSelector:@selector(showQRCodeView)]) {
        [self.viewDelegate showQRCodeView];
    }
}

//点击查看详情记录
- (void)inviteRecordButtonPress
{
    if (self.viewDelegate && [self.viewDelegate respondsToSelector:@selector(showInviteRecord)]) {
        [self.viewDelegate showInviteRecord];
    }
}

//点击立即邀请邻居
- (void)urlInviteButtonPress
{
    if (self.viewDelegate && [self.viewDelegate respondsToSelector:@selector(showUrlInviteView)]) {
        [self.viewDelegate showUrlInviteView];
    }
}

//点击查看更多
- (void)showMoreRuleButtonPress
{
    if (self.viewDelegate && [self.viewDelegate respondsToSelector:@selector(showInviteRules)]) {
        [self.viewDelegate showInviteRules];
    }
}
@end
